package com.xdcplus.web.service.wip.impl;

import com.xdcplus.mom.constants.NumberConstant;
import com.xdcplus.mom.enums.ResponseEnum;
import com.xdcplus.exceptions.XdcWebException;
import java.util.Arrays;
import cn.hutool.core.collection.CollectionUtil;

import com.xdcplus.mp.service.impl.BaseServiceImpl;
import com.xdcplus.web.mapper.WipMarkingElSummaryMapper;
import com.xdcplus.web.common.pojo.entity.WipMarkingElSummary;
import com.xdcplus.web.common.pojo.dto.WipMarkingElSummaryDTO;
import com.xdcplus.web.common.pojo.dto.WipMarkingElSummaryFilterDTO;
import com.xdcplus.web.common.pojo.vo.WipMarkingElSummaryVO;
import com.xdcplus.web.common.pojo.query.WipMarkingElSummaryQuery;
import org.springframework.beans.factory.annotation.Autowired;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.ObjectUtil;
import com.github.pagehelper.PageInfo;
import com.xdcplus.core.utils.BeanUtils;
import com.xdcplus.pager.vo.PageVO;
import com.xdcplus.mp.utils.PageableUtils;
import com.xdcplus.mp.utils.PropertyUtils;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.sql.Timestamp;

import com.xdcplus.web.service.wip.WipMarkingElSummaryService;
import org.springframework.stereotype.Service;
import lombok.extern.slf4j.Slf4j;

/**
 * (WipMarkingElSummary)表服务实现类
 *
 * @author bullion.yan
 * @since 2024-11-28 18:34:27
 */
@Slf4j
@Service("wipMarkingElSummaryService")
public class WipMarkingElSummaryServiceImpl extends BaseServiceImpl<WipMarkingElSummaryMapper, WipMarkingElSummary, WipMarkingElSummary, WipMarkingElSummaryVO> implements WipMarkingElSummaryService {

    @Autowired
    protected WipMarkingElSummaryMapper wipMarkingElSummaryMapper;

    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public Boolean saveWipMarkingElSummary(WipMarkingElSummaryDTO wipMarkingElSummaryDTO) {

        WipMarkingElSummary wipMarkingElSummary = new WipMarkingElSummary();
        BeanUtil.copyProperties(wipMarkingElSummaryDTO, wipMarkingElSummary);
        wipMarkingElSummary.setCreatedTime(new Timestamp(DateUtil.current()));

        return this.save(wipMarkingElSummary);
    }

    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public Boolean updateWipMarkingElSummary(WipMarkingElSummaryDTO wipMarkingElSummaryDTO) {

        WipMarkingElSummary wipMarkingElSummary = this.getById(wipMarkingElSummaryDTO.getId());
        if (ObjectUtil.isNull(wipMarkingElSummary)) {
            log.error("updateWipMarkingElSummary() The WipMarkingElSummary does not exist or has been deleted");
            throw new XdcWebException(ResponseEnum.ERROR);
        }

        BeanUtil.copyProperties(wipMarkingElSummaryDTO, wipMarkingElSummary);
        wipMarkingElSummary.setUpdatedUser(wipMarkingElSummaryDTO.getUpdatedUser());
        wipMarkingElSummary.setUpdatedTime(new Timestamp(DateUtil.current()));

        return this.updateById(wipMarkingElSummary);
    }

    @Override
    public Boolean saveOrUpdateBatch(List<WipMarkingElSummary> wipMarkingElSummaryList) {

        wipMarkingElSummaryList.forEach(wipMarkingElSummary -> {
            WipMarkingElSummary wipMarkingElSummaryParam = new WipMarkingElSummary();
            wipMarkingElSummaryParam.setId(wipMarkingElSummary.getId());
            if (ObjectUtil.isNotNull(wipMarkingElSummary.getId())) {
                wipMarkingElSummary.setId(wipMarkingElSummary.getId());
                wipMarkingElSummary.setUpdatedTime(new Timestamp(DateUtil.current()));
                LambdaUpdateWrapper<WipMarkingElSummary> lambdaUpdate = Wrappers.lambdaUpdate();
                lambdaUpdate.eq(WipMarkingElSummary::getId, wipMarkingElSummary.getId());
                update(wipMarkingElSummary, lambdaUpdate);
            } else {
                wipMarkingElSummary.setCreatedTime(new Timestamp(DateUtil.current()));
                save(wipMarkingElSummary);
            }
        });
        return true;
    }

    @Override
    public Boolean saveOrUpdateBatchByDTOList(List<WipMarkingElSummaryDTO> wipMarkingElSummaryDTOList) {

        List<WipMarkingElSummary> wipMarkingElSummaryList = BeanUtils.copyProperties(wipMarkingElSummaryDTOList, WipMarkingElSummary.class);
        return saveOrUpdateBatch(wipMarkingElSummaryList);
    }

    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public Boolean deleteWipMarkingElSummaryLogical(Long id) {

        Assert.notNull(id, ResponseEnum.THE_ID_CANNOT_BE_EMPTY.getMessage());

        WipMarkingElSummary wipMarkingElSummary = this.getById(id);

        if (ObjectUtil.isNull(wipMarkingElSummary)) {
            log.error("deleteWipMarkingElSummary() The WipMarkingElSummary does not exist or has been deleted");
            throw new XdcWebException(ResponseEnum.ERROR);
        }
        return this.removeById(id);
    }
    
    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public Boolean deleteWipMarkingElSummaryByIds(String ids) {
        Assert.notNull(ids, ResponseEnum.THE_ID_CANNOT_BE_EMPTY.getMessage());
		List<String> idList = Arrays.asList(ids.split(","));
		if(CollectionUtil.isEmpty(idList)){
           throw new XdcWebException(ResponseEnum.ERROR);
       }
		idList.forEach(id->{
			WipMarkingElSummary wipMarkingElSummary = this.getById(id);
			if (ObjectUtil.isNull(wipMarkingElSummary)) {
				log.error("deleteWipMarkingElSummary() The WipMarkingElSummary does not exist or has been deleted");
				throw new XdcWebException(ResponseEnum.ERROR);
			}
		});
		
        return this.removeByIds(idList);
    }

    private List<WipMarkingElSummary> queryWipMarkingElSummaryList(WipMarkingElSummaryFilterDTO wipMarkingElSummaryFilterDTO) {
        WipMarkingElSummaryQuery wipMarkingElSummaryQuery = BeanUtil.copyProperties(wipMarkingElSummaryFilterDTO, WipMarkingElSummaryQuery.class);

        return wipMarkingElSummaryMapper.queryWipMarkingElSummary(wipMarkingElSummaryQuery);
    }

    @Override
    public List<WipMarkingElSummaryVO> queryWipMarkingElSummaryVOList(WipMarkingElSummaryFilterDTO wipMarkingElSummaryFilterDTO) {
        return this.objectConversion(queryWipMarkingElSummaryList(wipMarkingElSummaryFilterDTO));
    }

    @Override
    public PageVO<WipMarkingElSummaryVO> queryWipMarkingElSummary(WipMarkingElSummaryFilterDTO wipMarkingElSummaryFilterDTO) {
        PageVO<WipMarkingElSummaryVO> pageVO = new PageVO<>();

        if (wipMarkingElSummaryFilterDTO.getCurrentPage() > NumberConstant.ZERO) {
            PageableUtils.basicPage(wipMarkingElSummaryFilterDTO.getCurrentPage(), wipMarkingElSummaryFilterDTO.getPageSize(),
                    wipMarkingElSummaryFilterDTO.getOrderType(), wipMarkingElSummaryFilterDTO.getOrderField());
        }

        List<WipMarkingElSummary> wipMarkingElSummaryList = queryWipMarkingElSummaryList(wipMarkingElSummaryFilterDTO);

        PageInfo<WipMarkingElSummary> pageInfo = new PageInfo<>(wipMarkingElSummaryList);
        PropertyUtils.copyProperties(pageInfo, pageVO, this.objectConversion(wipMarkingElSummaryList));

        return pageVO;
    }

    @Override
    public WipMarkingElSummaryVO queryWipMarkingElSummaryById(Long id) {

        Assert.notNull(id, ResponseEnum.THE_ID_CANNOT_BE_EMPTY.getMessage());

        return this.objectConversion(this.getById(id));
    }

    @Override
    public List<WipMarkingElSummaryVO> queryByMarkingIdAndReportType(Long markingId, String reportType) {
        return this.objectConversion(wipMarkingElSummaryMapper.queryByMarkingIdAndReportType(markingId,reportType));
    }


}
